var app = getApp();
import { showActionSheet } from '../../utils/camera.js';
Page({
    data: {
        currentx:0,
        currenty:0,
        initx:0,
        inity:0,
        endx:0,
        endy:0,
        mx:0,
        my:0,
        opacity:1,
        src:1,
        jia:{'src':'/images/1.jpg'},
        list:[{'src':'/images/1.jpg'},{'src':'/images/2.jpg'},{'src':'/images/3.jpg'},{'src':'/images/4.jpg'}],
        main:0,
        tabs: [{ "name": "最新", "type": 'new' }, { "name": "最热", "type": "hot" }],
        activeIndex:0,
        tabBar:[{ "name": "留言板",'icon':'/images/liu.png','url':'../home/home','selectIcon':'/images/mine.png' },{ "name": "我的",'icon':'/images/mine.png','url':'../mine/mine','selectIcon':'/images/liu.png' }],
        moveIndex:0,
        start:false,
        newList:[],
        info:{},
        hotList:[],
        activeList:[],
        activeImg:null,
        ineedmt:0,
        flag:false,
        first:{}
    },
    onLoad: function () {
        console.log('wuyu');
    },
    ballMoveEvent: function (e) {
        console.log('我被拖动了....')
        var touchs = e.touches[0];
        var pageX = touchs.pageX;
        var pageY = touchs.pageY;
        console.log('pageX: ' + pageX)
        console.log('pageY: ' + pageY)
        this.setData({
            currentx: pageX,
            currenty: pageY
        })
        var x =this.data.currentx - this.data.initx;
        var y =this.data.currenty - this.data.inity;
        this.setData({
            mx: x,
            my: y
        }) 
    },

    //点击事件 
    touchstart: function (e) {
        var touchs = e.touches[0];
        var pageX = touchs.pageX;
        var pageY = touchs.pageY;
        this.setData({ 
            initx: pageX,
            inity: pageY
        }); 
    },

        //点击事件
    touchend: function (e) {
        this.setData({ 
            endx: this.data.currentx, 
            endy: this.data.currenty 
        }); 
        if((Math.abs(this.data.endx - this.data.initx) > 90) || (Math.abs(this.data.endy - this.data.inity) > 90)){//判断离开 还是还原
            this.move(e);
        }else{
            this.inint();
        }

    },

    inint: function () {
        var xx = this.data.mx;
        var yy = this.data.my;
        var stepx = xx / 100;
        var stepy = yy / 100;
        var that = this;
        var i = 100;
        var timer = setInterval(function(){
            i --;
            if(i>0){

                that.setData({ 
                    mx: that.data.mx-stepx,
                    my: that.data.my-stepy
                }); 
            }else{
                clearInterval(timer);
            }
        },5)
    },

    move: function (e) {
        console.log('离开');
        var xx = this.data.mx;
        var yy = this.data.my;
        var stepx = xx / 100;
        var stepy = yy / 100;
        var that = this;
        var i = 200;
        var timer = setInterval(function(){
            i --;
            if(i>0){
                that.setData({ 
                    mx: that.data.mx+stepx,
                    my: that.data.my+stepy
                }); 
            }else{
                clearInterval(timer);
            }
        },5)
        console.log(e)
        var timer2 = setInterval(()=>{
            if(that.data.opacity <= 0){
                clearInterval(timer2);
                    this.setData({ 
                    opacity: 0,
                });
                this.wang();
                return;
            }
            that.setData({ 
                opacity: that.data.opacity-0.1,
            });
        },100)
        setTimeout(()=>{
            
        })
    },

    wang: function(){ 
    if(this.data.activeIndex==0){
        this.setData({ 
            currentx:0,
            currenty:0,
            initx:0,
            inity:0,
            endx:0,
            endy:0,
            mx:0,
            my:0,
            opacity:1,
            src:this.data.src+1,
            first:this.data.newList[this.data.src]
        });
    }else{
        this.setData({ 
            currentx:0,
            currenty:0,
            initx:0,
            inity:0,
            endx:0,
            endy:0,
            mx:0,
            my:0,
            opacity:1,
            src:this.data.src+1,
            first:this.data.hotList[this.data.src]
        });
    }
    },
    clickNum:function(e){
        var self=this
        if(e.currentTarget.id==1){
            self.hotFun()
        }else{
            self.newFun()
        }
        self.setData({
            activeIndex: e.currentTarget.id,
            main:e.currentTarget.id
        })
    },
    clickTab:function(e){
        console.log(e);
        var self=this
        var list=self.data.tabBar
        var index=e.currentTarget.id
        console.log(list[2])
        self.setData({
            moveIndex:index ,
        })
        wx.redirectTo({
            url: list[index].url
        })
        
        
  },
  camera: function (e) {
    var self = this
    showActionSheet({ name: 'headimg', isOne: 1, type: { "type": 'headimg' } }).then(data => {
      var user = data.user
      wx.setStorageSync('wechat_service_user', user)
      app.globalData.userInfo = user
      self.setData({
        user: user
      })
    })
  },
  onPullDownRefresh: function(){
    var self=this
    self.getBrandInfo()
    self.setData({
        start:true
    })
    wx.stopPullDownRefresh()
  },
  onReachBottom:function(){
      var self=this
    self.setData({
        start:false
    })
  },
  onReady:function(){
      var that=this
      var value=wx.getStorageSync('userInfo')
      that.setData({
          info:value
      })
      this.newFun()
      this.getBrandInfo()
  },
  hotFun:function(){
      var value=wx.getStorageSync('userInfo')
    var that=this
    wx.request({
        url: 'https://www.vaniswdq.com/getInfoByBrandHot',
        data: {
            brandId:'1',
            uuid:value.uuid
        },
        header: {
            'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
            var cardList=res.data.dataWrapper.tbLatestWords
            console.log(cardList)
            that.setData({
                hotList:cardList,
                first:cardList[0]
            })
        }
    })
  },
  newFun:function(){
    var value=wx.getStorageSync('userInfo')
    var that=this
    wx.request({
        url: 'https://www.vaniswdq.com/getInfoByBrandLatest',
        data: {
            brandId:'1',
            uuid:value.uuid
        },
        header: {
            'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
            var cardList=res.data.dataWrapper.tbLatestWords
            console.log(cardList)
            that.setData({
                newList:cardList,
                first:cardList[0]
            })
        }
    })
  },
  savePraise:function(e){
      var value=wx.getStorageSync('userInfo')
      var that=this
      console.log(e)
      that.setData({
          ineedmt:e.target.dataset.zanindex,
          flag:e.target.dataset.zanflag
      })
      if(e.target.dataset.zanflag=='true'){
            wx.request({
            url: 'https://www.vaniswdq.com/savePraise',
            data: {
                id:e.currentTarget.id,
                uuid:value.uuid
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                console.log(res.data)
                if(res.data.status=="0"){
                    var newList=that.data.newList
                    newList[that.data.ineedmt].praise=that.data.flag
                    that.setData({
                        newList:newList
                    })
                    that.newFun()
                    that.hotFun()
                }else{
                    wx.showModal({
                        title: '提示',
                        content: res.data.msg,
                        showCancel:false
                        })
                }
            }
        }) 
      }
     
  },
  getBrandInfo:function(e){
    var self=this
    wx.request({
        url: 'https://www.vaniswdq.com/getBrandInfo',
        data: {
            brandId:'1'
        },
        header: {
            'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
            // console.log(res.data)
            var brandList=res.data.dataWrapper.tbBrand.resultList
            self.setData({
                activeList:brandList
            })
        }
    }) 
  },
  saveRead:function(e){
    var self=this
    console.log(e)
    wx.request({
        url: 'https://www.vaniswdq.com/saveReadNum',
        data: {
            id:e.currentTarget.id
        },
        header: {
            'content-type': 'application/x-www-form-urlencoded'
        },
        success: function(res) {
            if(self.data.activeIndex==0){
                self.newFun()
            }else{
                self.hotFun()
            }
        }
    }) 
  }
}) 